home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Devices and Hardware / Disks / iso9660 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  8.3 KB  |  387 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        main.c
  3.     
  4.     Description:This module contains all the Macintosh flow control
  5.                  for the ISO9660 floppy program.
  6.  
  7.     Author:
  8.  
  9.     Copyright:     Copyright: © 1990-1999 by Apple Computer, Inc.
  10.                 all rights reserved.
  11.     
  12.     Disclaimer:    You may incorporate this sample code into your applications without
  13.                 restriction, though the sample code has been provided "AS IS" and the
  14.                 responsibility for its operation is 100% yours.  However, what you are
  15.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  16.                 after having made changes. If you're going to re-distribute the source,
  17.                 we require that you make it clear in the source that the code was
  18.                 descended from Apple Sample Code, but that you've made changes.
  19.     
  20.     Change History (most recent first):
  21.                 6/24/99    Updated for Metrowerks Codewarror Pro 2.1(KG)
  22.  
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <QuickDraw.h>
  27. #include <Menus.h>
  28. #include <Memory.h>
  29. #include <Events.h>
  30. #include <Windows.h>
  31. #include <Dialogs.h>
  32. #include <Desk.h>
  33. #include <Files.h>
  34. #include <ToolUtils.h>
  35. #include <DiskInit.h>
  36. #include <Fonts.h>
  37. #include <Menus.h>
  38.  
  39. #include "HighSierra.h"
  40. #include "BuildISO.h"
  41. #include "DialogUtils.h"
  42. #include "BuildISO.h"
  43. #include "ErrorMsg.h"
  44. #include "i_o.h"
  45.  
  46. QDGlobals    qd;
  47.  
  48. Str255            drvName = "\p.Sony";        /* name of the device driver for isoOpen() */
  49.  
  50. enum {
  51.     AppleID = 128,
  52.     FileID,
  53.     EditID
  54. };
  55.  
  56. enum {
  57.     AppleM = 1,
  58.     FileM,
  59.     EditM
  60. };
  61.  
  62. #define    PROMPTID    258
  63.  
  64. #define    HELP        1
  65. #define CREATEFILES    2
  66. #define    QUIT        4
  67.  
  68. #define MENUCOUNT    3
  69.  
  70. #include "main.h"
  71. #include "MyDialog.h"
  72.  
  73. static void DoDisk(long message);
  74. static void InitializeVolume(void);
  75.  
  76. MenuHandle    gMenus[MENUCOUNT];
  77. Boolean        gQuit;
  78. EventRecord    gEvent;
  79. WindowPtr    gWindow;
  80.  
  81. short    gRefNum;
  82. short    gDriveNumber = 0;
  83.  
  84. static void DoDisk(long);
  85. static void    InitializeVolume(void);
  86.  
  87. void SetUpMenus(void);
  88. void DoAbout(void);
  89. void DoCommand(long);
  90. void HandleEvent(EventRecord *);
  91. void main(void);
  92.  
  93.  
  94. /************************************************************************
  95.  *
  96.  *  Function:        SetUpMenus
  97.  *
  98.  *  Purpose:        initialize menus
  99.  *
  100.  *  Returns:        nothing
  101.  *
  102.  *  Side Effects:    standard apple menus are set up
  103.  *
  104.  *  Description:    loop through MENUCOUNT times, inserting the menu
  105.  *                    we got using GetMenu.  Draw the menu bar.
  106.  *
  107.  ************************************************************************/
  108. void
  109. SetUpMenus(void)
  110. {
  111.     short    i;
  112.     
  113.     gMenus[AppleM] = GetMenu(AppleID);
  114.     AppendResMenu(gMenus[AppleM], 'DRVR');    /* add desk accessories */
  115.     gMenus[FileM] = GetMenu(FileID);
  116.     gMenus[EditM] = GetMenu(EditID);        /* only used by desk access. */
  117.     
  118.     for (i = 1; i <= MENUCOUNT; i++)
  119.         InsertMenu(gMenus[i], 0);    /*install at end of Menu Bar */
  120.  
  121.     /* disable creating files until we initialize a floppy */
  122. /***TO DO***    DisableItem(gMenus[FileM], CREATEFILES); */
  123.     DrawMenuBar();
  124. }
  125.  
  126.  
  127. /************************************************************************
  128.  *
  129.  *  Function:        DoAbout
  130.  *
  131.  *  Purpose:        give ownership & copyright information
  132.  *
  133.  *  Returns:        nothing
  134.  *
  135.  *  Side Effects:    nothing
  136.  *
  137.  *  Description:    put up an alert, telling about us.
  138.  *
  139.  ************************************************************************/
  140. void
  141. DoAbout(void)
  142. {
  143.     Alert(DU_CenterALRT(128), 0L);
  144. }
  145.  
  146.  
  147.  
  148. /************************************************************************
  149.  *
  150.  *  Function:        DoCommand
  151.  *
  152.  *  Purpose:        Handle command or command key equivalent
  153.  *
  154.  *  Returns:        void
  155.  *
  156.  *  Side Effects:    whatever it executes may have a side effect.
  157.  *
  158.  *  Description:    Look at the menu selected and the item of that menu,
  159.  *                    and do a case statement on the item.  Execute the
  160.  *                    appropriate item.
  161.  *
  162.  ************************************************************************/
  163. void
  164. DoCommand(long mResult)
  165. {
  166.     short    theItem;
  167.     short    theMenu;
  168.     Str255 name;
  169.     GrafPtr savePort;
  170.     
  171.     theItem = mResult &0xFFFF;
  172.     theMenu = (mResult >> 16) & 0xFFFF;
  173.     
  174.     switch (theMenu) {
  175.     case AppleID:
  176.         if (theItem == 1)     /* tell about the program */
  177.             DoAbout();
  178.         else {
  179.             GetPort(&savePort);
  180.             GetMenuItemText(gMenus[AppleM], theItem, name);
  181.             OpenDeskAcc(name);
  182.             SetPort(savePort);
  183.         }
  184.         break;
  185.     case FileID:
  186.         switch (theItem)
  187.         {
  188.             case    HELP:
  189.                 Help();
  190.                 break;
  191.             case    CREATEFILES:
  192.                 CreatePathTable(gRefNum);
  193.                 CreateFiles(gRefNum);
  194.                 /* DisableItem(gMenus[FileM], CREATEFILES); */
  195.                 break;
  196.             case    QUIT:
  197.                 gQuit = true;
  198.                 break;
  199.         }
  200.     case EditID:
  201.         SystemEdit(theItem - 1);
  202.         break;
  203.     }
  204.     HiliteMenu(0);
  205. }
  206.  
  207.  
  208.  
  209. /************************************************************************
  210.  *
  211.  *  Function:        DoDisk
  212.  *
  213.  *  Purpose:        Handle disk inserted events
  214.  *
  215.  *  Returns:        nothing
  216.  *
  217.  *  Side Effects:    may initialize a disk
  218.  *
  219.  *  Description:    check the return code from the mount attempt (the
  220.  *                    high word of the event message coming in.)  If the
  221.  *                    mount failed, go ahead and initialize it using DIFormat.
  222.  *                    This gets the basic format information, but no file
  223.  *                    system information.
  224.  *
  225.  ************************************************************************/
  226.  static void
  227.  DoDisk(long message)
  228.  {
  229.      OSErr    result;
  230.      CursHandle    cursor;
  231.      
  232.      gDriveNumber = message & 0xFFFF;
  233.      result = (message >> 16) & 0xFFFF;
  234.      
  235.      /* if a mount failed (e.g. blank floppy) then we can format the floppy
  236.      ** without doing anything.  If the mount succeeded, the floppy was in
  237.      ** some format that the Mac recognized.  Unmount the volume and flush
  238.      ** it out of the mac without ejecting it, and *then* format it.
  239.      */
  240.      if (result == noErr)
  241.      {
  242.          if (AskDestroyDisk(gDriveNumber) == true)
  243.              result = UnmountVol(NULL, gDriveNumber);
  244.          else
  245.          {
  246.              UnmountVol(NULL, gDriveNumber);
  247.              Eject(NULL, gDriveNumber);
  248.              return;                /* get rid of that volume and return now! */
  249.          }
  250.      }
  251.      
  252.      cursor = GetCursor(watchCursor);
  253.      if (!cursor)
  254.          SetCursor(*cursor);
  255.      result = DIFormat(gDriveNumber);
  256.     InitializeVolume();
  257.     EnableItem(gMenus[FileM], CREATEFILES);
  258.     SetCursor(&qd.arrow);
  259.          
  260.      if (result != noErr)
  261.          ErrorMsg("Can't format volume! (result = %d)", result);
  262. }
  263.  
  264.  
  265.  
  266. void
  267. HandleEvent(EventRecord *gEvent)
  268. {
  269.     DialogPtr    dPtr;
  270.     short        itemHit;
  271.     
  272.     switch (gEvent->what) {
  273.     case mouseDown:
  274.         switch (FindWindow(gEvent->where, &gWindow)) {
  275.         case inSysWindow:
  276.             SystemClick(gEvent, gWindow);
  277.             break;
  278.         case inMenuBar:
  279.             DoCommand(MenuSelect(gEvent->where));
  280.             break;
  281.         }    /* end MouseDown */
  282.         break;
  283.     case keyDown:
  284.     case autoKey:
  285.         if ((gEvent->modifiers & cmdKey) != 0)
  286.             DoCommand (MenuKey ((char) (gEvent->message & 0xFF)));
  287.         break;
  288.     case diskEvt:
  289.         DoDisk(gEvent->message);
  290.         break;
  291.     case updateEvt:            /* These can only happen to our dialogs */
  292.     case activateEvt:
  293.         DialogSelect(gEvent, &dPtr, &itemHit);
  294.     }    /* end gEvent.what */
  295. }
  296.  
  297. /************************************************************************
  298.  *
  299.  *  Function:        main
  300.  *
  301.  *  Purpose:        control this puppy
  302.  *
  303.  *  Returns:        none    
  304.  *
  305.  *  Side Effects:    none inherient in this routine.  Routines called
  306.  *                    from here do all the work.
  307.  *
  308.  *  Description:    Do standard Macintosh initialization.
  309.  *
  310.  ************************************************************************/
  311. void
  312. main(void)
  313. {
  314.     OSErr        result;
  315.     DialogPtr    dPtr;
  316.     
  317.     
  318.     InitGraf((Ptr)&qd.thePort);
  319.     InitFonts();
  320.     FlushEvents(everyEvent, 0);
  321.     InitWindows();
  322.     InitMenus();
  323.     TEInit();
  324.     InitDialogs(0L);
  325.     InitCursor();
  326.     MoreMasters();
  327.     
  328.     SetUpMenus();
  329.  
  330.     gRefNum = 0;
  331.     result = isoOpen(drvName, &gRefNum);
  332.     if (result != noErr)
  333.     {
  334.         ErrorMsg("isoOpen failed with code %d (0x%x)", result, result);
  335.         return;
  336.     }
  337.  
  338.     gQuit = false;
  339.     
  340.     dPtr = GetNewDialog(DU_CenterDLOG(PROMPTID), (DialogPeek)0L, (WindowPtr)-1L);
  341.     do {
  342.         SystemTask();
  343.         if (GetNextEvent(everyEvent, &gEvent)) {
  344.                 HandleEvent(&gEvent);
  345.         }    /* end if */
  346.     } while (gQuit == false);
  347.     DisposeDialog(dPtr);
  348.  
  349.     if (gDriveNumber != 0)
  350.     {
  351.         result = Eject(NULL, gDriveNumber);
  352.         if (result != noErr)
  353.             ErrorMsg("can't eject floppy! (%d)", result);
  354.     }
  355. }
  356.  
  357.  
  358. /************************************************************************
  359.  *
  360.  *  Function:        InitializeVolume
  361.  *
  362.  *  Purpose:        initialize volume
  363.  *
  364.  *  Returns:        nothing
  365.  *
  366.  *  Side Effects:    destroys volume and create PVD and VDT instead
  367.  *
  368.  *  Description:
  369.  *
  370.  ************************************************************************/
  371. static void
  372. InitializeVolume(void)
  373. {
  374.     OSErr    result;
  375.     
  376.     result = ZeroDisk(gRefNum);
  377.     if (result == noErr)
  378.     {
  379.         result = CreatePVD(gRefNum);
  380.         if (result == noErr)
  381.             result = CreateVDT(gRefNum);
  382.     }
  383.     if (result != noErr)
  384.         ErrorMsg("Couldn't initialize volume");
  385. }
  386.         
  387.